package jsp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:石小俊
 * @Date:2022/12/14 11:21
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
@WebServlet("/count")
public class CountServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取session作用域中的count的数据
        Integer count = (Integer) request.getSession().getAttribute("count");
        // 如果返回的count的值是null,表示没有存储过,说明是第一次访问
        if(null == count){
            // 当第一次访问时,设置默认值为1
            count = 1;
        }else{
            // 如果不是第一次访问,则在原始数值上+1
            count++;
        }
        request.getSession().setAttribute("count",count);
        request.getRequestDispatcher("/session/count.jsp").forward(request,response);
    }

}
